package com.xnsy.jishexiangmu.service.user.impl;

import com.xnsy.jishexiangmu.mapper.UserMapper;
import com.xnsy.jishexiangmu.pojo.JuBao;
import com.xnsy.jishexiangmu.pojo.RenZheng;
import com.xnsy.jishexiangmu.pojo.UserMessage;
import com.xnsy.jishexiangmu.service.user.UserService;
import com.xnsy.jishexiangmu.uitl.Md5Util;
import com.xnsy.jishexiangmu.pojo.User;
import com.xnsy.jishexiangmu.uitl.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByUsername(String username) {
        User user = userMapper.findByUsername(username);
        return user;
    }

    @Override
    public void register(String username, String password) {
        // 密码进行加密代码
            String md5Password = Md5Util.getMD5String(password);
            userMapper.register(username, md5Password);
            userMapper.createUserMessage(username);
    }

    @Override
    public void updatePassword(String newpassword, String username) {
        String md5password = Md5Util.getMD5String(newpassword);
        userMapper.updatePassword(md5password, username);
    }

    @Override
    public UserMessage getUserMessage() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        return userMapper.getUserMessage(username);
    }

    @Override
    public void updateUserMessage(UserMessage userMessage) {
        userMapper.updateUserMessage(userMessage);
    }

    @Override
    public void renzheng(String renzheng, String toWhere) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        userMapper.renzheng(renzheng, username, toWhere);
    }

    @Override
    public List<RenZheng> getrenzheng() {
        List<RenZheng> renZhengs = userMapper.getrenzheng();
        return renZhengs;
    }

    @Override
    public void setRengZheng(String username, Integer isaccept, String authentication, String toWhere) {
        userMapper.setRenZheng(username, isaccept);
        userMapper.setAuthentication(username, authentication, toWhere);
        // 待编写用户所属哪个端
    }

    @Override
    public List<User> getAllUser(String s) {
        List<User> users = userMapper.getAllUser(s);
        return users;
    }

    @Override
    public void setRoot(Integer isroot, Integer uid) {
        userMapper.setRoot(isroot,uid);
    }

    @Override
    public void postMessage(JuBao juBao) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        juBao.setUsername(username);
        userMapper.postMessage(juBao);
    }

    @Override
    public int getJinDu() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        return userMapper.getJinDu(username);
    }

    @Override
    public List<JuBao> getJuBao() {
        return userMapper.getJuBao();
    }

    @Override
    public void setJinDu(Integer id) {
        userMapper.setJinDu(id);
    }
}
